<template>
	<div class="evidence-unit-casePrint-container">
		<el-dialog title="案件编辑" v-model="isShowDialog" width="860px"  destroy-on-close >
			<AddCase ref="refAddCase" :formInline="formInline" />
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="small">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="small" :disabled="!isSameOrgan">编 辑</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref ,watchEffect} from 'vue';
import AddCase from '/@/views/evidence/add/component/addCase.vue';
import { putCase } from '/@/api/case/index';
import { ElMessage } from 'element-plus';
import { initDic } from '/@/common';
export default {
	name: 'editCase',
	props: {
		formInline: Object
	},
	components: { AddCase },
	emits: ['reload'],
	setup(props, { emit }) {
		const state = reactive({
			isShowDialog: false,
			isSameOrgan:true
		});
		// 打开弹窗
		const openDialog = (sameOrgan) => {
			state.isShowDialog = true;
			if(sameOrgan != undefined){
				state.isSameOrgan = sameOrgan;
			}
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		const refAddCase = ref();
		async function onSubmit() {
			await refAddCase.value.refAddCaseForm.validate((valid) => {
				if (valid) {
					props.formInline.caseTypeName = options.ajlbs.find((x) => x.value == props.formInline.caseTypeCode)?.label;
					props.formInline.hostUserId = options.tqrs.find((x) => x.label == props.formInline.hostUserName)?.id || 0;
					props.formInline.hostUserOrgan = options.tqdws.find((x) => x.label == props.formInline.hostUserOrganName)?.id || 0;
					putCase(props.formInline).then((res) => {
						if (res.success) {
							state.isShowDialog = false;
							emit('reload');
							ElMessage({ message: '操作成功', type: 'success' });
						}
					});
				}
			});
		}

		const options = reactive({
		tqrs: [],
		tqdws: [],
		ajlbs: [],
	});


      initDic(options);
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			refAddCase,
			...toRefs(state),
		};
	},
};
</script>
